<template>
    <!-- 注册页面的整体盒子 -->
    <div class="reg-container">
      <!-- 注册的盒子 -->
      <div class="reg-box">
        <!-- 标题的盒子 -->
        <div class="title-box"></div>
        <!-- 注册的表单区域 -->
        <!-- 
            :model="formData"   表单收集数据的对象
            :rules="formRules"   验证规则对象
            ref="FormTef"       兜底校验
          -->
        <el-form :model="formData" :rules="formRules" ref="FormRef"  style="padding: 0 25px">
            <el-form-item prop="username">
                <el-input  v-model="formData.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
            </el-form-item>

            <el-form-item prop="password">
                <el-input v-model="formData.password" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item prop="repassword">
                <el-input v-model="formData.repassword" prefix-icon="el-icon-lock" placeholder="请确认密码"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button style="width:100%" type="primary" @click="resNewForm">注册</el-button>
                <el-link type="info">去登录</el-link>
            </el-form-item>
        </el-form>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    name: 'Reg',
    data(){
        // 自定义密码校验  如果第二次确认的密码和第一次不一样 就提示用户
        var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.formData.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return {
            formData:{
                username:'',
                password:'',
                repassword:''
            },
            formRules:{
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { pattern:/^[1-9a-zA-Z]{1,9}$/, message: '请输入1-9的字母或数字', trigger: 'blur' },
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    // pattern  定义正则规则
                    { pattern:/^[\S]{6,15}$/, message: '请输入6-15的密码', trigger: 'blur' },
                ],
                repassword:[
                    { required: true, message: '请确定密码', trigger: 'blur' },
                    { pattern:/^[\S]{6,15}$/, message: '请确认6-15的密码', trigger: 'blur' },
                    { validator: validatePass2, trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        resNewForm(){
            //  valid返回值是boolean 只有所有的表单项验证通过返回的是true
            //1.  只要一个表单项没有验证通过，返回false
            this.$refs.FormRef.validate( async(valid) => {
                // console.log(valid);
           //   //1.1 注册失败 ,给出提示
            if (!valid)   console.log(res.message);
                // 回调函数是promise 的实例对象
              const {data:res}  =  await this.$http.post('/api/reg',this.formData)
              console.log(res);
              // 2.1  注册失败,给出提示
            if(res.code!==0)   this.$message.error(res.message);
            // 2.2 注册成功 ,给出提示
            this.$message.success(res.message);
              //3. 成功之后 跳转到登录页面
              this.$router.push('/login')
            } )
        }
        }
    }
  
  </script>
  
  <style lang="less" scoped>
  .reg-container {
    background: url('../../assets/images/login_bg.jpg') center;
    background-size: cover;
    height: 100%;
  
    .reg-box {
      width: 400px;
      height: 335px;
      background-color: #fff;
      border-radius: 3px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
  
      .title-box {
        height: 60px;
        background: url('../../assets/images/login_title.png') center no-repeat;
      }
    }
  }
  </style>